package com.sea.operate.controller;

import com.codecrane.core.power.service.DictDetailService;
import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.ExcelUtil;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.operate.entity.Related;
import com.sea.operate.entity.RelatedMaterial;
import com.sea.operate.service.RelatedMaterialService;
import com.sea.operate.service.RelatedService;
import com.sea.operate.service.RelatedSkuService;
import com.sea.product.service.ChannelService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;


/**
 *  控制器
 * @author 陈鸿飞
 *  <br/>
 *  <br/>
 */
@Controller
@RequestMapping("/sea/operate/related/material")
public class RelatedMaterialController {

    @Autowired
    private ChannelService channelService;
    @Autowired
    private RelatedMaterialService relatedMaterialService;
    @Autowired
    private DictDetailService dictDetailService;



    /**
     * 管理主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        model.addAttribute("channel",channelService.findByCnd(null));

        return "/sea/operate/relatedMaterial/main";
    }

    @RequestMapping("/editPage")
    public String editPage(Model model,
                           @RequestParam("channel") String channel,
                           @RequestParam("listing")String listing) {


        model.addAttribute("FLLX", dictDetailService.findByTypeCode("FLLX", null));
        model.addAttribute("MBLX", dictDetailService.findByTypeCode("MBLX", null));
        model.addAttribute("channel", channel);
        model.addAttribute("listing", listing);
        RelatedMaterial relatedMaterial=new RelatedMaterial();
        relatedMaterial.setRelatedListing(listing);
        relatedMaterial.setRelatedChannelName(channel);
        Integer save = relatedMaterialService.save(relatedMaterial);

        model.addAttribute("id",save);
        return "/sea/operate/relatedMaterial/edit";
    }
    @RequestMapping("/addPage")
    public String addPage(Model model) {

        return "/sea/operate/relatedMaterial/add";
    }
    @RequestMapping("/detailPage")
    public String detailPage(Model model, @RequestParam("id") Integer id) {
        model.addAttribute("id",id);
        return "/sea/operate/relatedMaterial/detail";
    }


    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     RelatedMaterial relatedMaterial) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<RelatedMaterial> relatedList = relatedMaterialService.findByCnd(relatedMaterial);
        PageInfo<RelatedMaterial> page = new PageInfo<>(relatedList);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }


}
